
(function(window,document,Laya){
	var __un=Laya.un,__uns=Laya.uns,__static=Laya.static,__class=Laya.class,__getset=Laya.getset,__newvec=Laya.__newvec;

	var ColorPickerInput=laya.ide.props.ColorPickerInput,Event=laya.events.Event,FileManager=laya.ide.managers.FileManager;
	var IDEEvent=laya.ide.event.IDEEvent,Image=laya.ui.Image,Label=laya.ui.Label,Notice=ide.managers.Notice,PropsInputBase=laya.ide.props.PropsInputBase;
	var ResFileManager=laya.ide.managers.ResFileManager,Sprite=laya.display.Sprite,StyleConsts=laya.ide.config.StyleConsts;
	//class laya.ide.props.LayaMaterialUI extends laya.ide.props.PropsInputBase
	var LayaMaterialUI=(function(_super){
		function LayaMaterialUI(){
			this.sizeGrid="5,15,5,5";
			this.useAssetsPath=true;
			this._$7_target=null;
			this.assetsPath=null;
			this.sprites=[];
			this.lmatPaths=[];
			this.lmatObjects=[];
			this.lmatProps=[];
			LayaMaterialUI.__super.call(this);
			this.useIDELabel=false;
		}

		__class(LayaMaterialUI,'laya.ide.props.LayaMaterialUI',_super);
		var __proto=LayaMaterialUI.prototype;
		__proto.onChangeColor=function(index,type,e){
			var vectors=this.lmatProps[index].vectors;
			var vector;
			var sendColor;
			for (var j=0;j < vectors.length;j++){
				vector=vectors[j];
				if (vector.name=="albedoColor"){
					if (type=="albedoColor"){
						sendColor=vector.value=this.sprites[index].albedoColor.getColorArr();
					}
				}
			}
			Notice.notify("Change_Material",[this._$7_target.compId,index,type,sendColor]);
		}

		//FileManager.createJSONFile(lmatPath,lmatObject);
		__proto.onDragDrop=function(index,type,e){
			var path=e._data.asset;
			var texturePath=path.replace('\\','/');
			if (type=="albedoTexture"){
				this.sprites[index].albedoTexture.skin=texturePath;
			};
			var lmatPath=this.lmatPaths[index];
			var findTexture=false;
			var textures=this.lmatProps[index].textures;
			var texture;
			for (var i=0;i < textures.length;i++){
				texture=textures[i];
				if (texture.name==type){
					texture.path=FileManager.getRelativePath(lmatPath.substring(0,lmatPath.lastIndexOf('/')),path);
					findTexture=true;
				}
			}
			if (!findTexture){
				texture=new Object();
				texture.name=type;
				texture.path=FileManager.getRelativePath(lmatPath.substring(0,lmatPath.lastIndexOf('/')),path);
				textures.push(texture);
			}
			Notice.notify("Change_Material",[this._$7_target.compId,index,type,path]);
		}

		//FileManager.createJSONFile(lmatPath,lmatObject);
		__proto.onMouseDown=function(index,type,e){
			var path;
			if (type=="albedoTexture"){
				path=this.sprites[index].albedoTexture.skin;
			}
			path=path.replace('\\','/');
			Notice.notify("SELECT_Project_FILE",[path]);
		}

		__proto.setTarget=function(target){
			_super.prototype.setTarget.call(this,target);
			this._$7_target=target;
			this.removeChildren();
			this.sprites.length=0;
			this.lmatPaths.length=0;
			this.lmatObjects.length=0;
			this.lmatProps.length=0;
			this.assetsPath=ResFileManager.getIDEResPath("");
			var materials=target.props.materials;
			if (!materials)return;
			var materialLength=materials.length;
			this.height=materialLength *40;
			this.width=400;
			for (var i=0;i < materialLength;i++){
				var lmatPath=materials[i].path;
				lmatPath=FileManager.getPath(this.assetsPath,lmatPath);
				this.lmatPaths.push(lmatPath);
				var sprite=this.addChild(new Sprite());
				sprite.pos(0,i *40);
				this.sprites.push(sprite);
				this.addMaterialSprite(sprite,i,lmatPath,materials[i]);
			}
		}

		__proto.addMaterialSprite=function(sprite,index,lmatPath,materialO){
			var albedoLabel=new Label();
			albedoLabel.text="Material"+index;
			albedoLabel.color=StyleConsts.TreeItemColor;
			albedoLabel.pos(0,8);
			sprite.addChild(albedoLabel);
			var albedoTexture=new Image("comp/image.png");
			albedoTexture.width=30;
			albedoTexture.height=30;
			albedoTexture.pos(75,0);
			albedoTexture.mouseEnabled=true;
			albedoTexture.on("dragDrop",this,this.onDragDrop,[index,"albedoTexture"]);
			albedoTexture.on("mousedown",this,this.onMouseDown,[index,"albedoTexture"]);
			sprite.albedoTexture=albedoTexture;
			sprite.addChild(albedoTexture);
			var albedoColor=new ColorPickerInput();
			albedoColor.width=225;
			albedoColor.height=25;
			albedoColor.pos(160,3);
			albedoColor.setColorArr([1,1,1,1]);
			albedoColor.on("ColorChanged",this,this.onChangeColor,[index,"albedoColor"]);
			albedoColor.on("COLOR_CHANGING",this,this.onChangeColor,[index,"albedoColor"]);
			sprite.albedoColor=albedoColor;
			sprite.addChild(albedoColor);
			if (FileManager.exists(lmatPath)){
				var lmatObject=FileManager.readJSONFile(lmatPath);
				this.lmatObjects.push(lmatObject);
				var lmatProp=lmatObject.props;
				this.lmatProps.push(lmatProp);
				var textures=lmatProp.textures;
				var texture;
				for (var i=0;i < textures.length;i++){
					texture=textures[i];
					if (texture.name=="albedoTexture"){
						var albedoTexturePath=FileManager.getPath(this.assetsPath,texture.path);
						if (!FileManager.exists(albedoTexturePath)){
							albedoTexturePath=FileManager.getPath(lmatPath,"../"+texture.path);
						}
						albedoTexture.skin=albedoTexturePath;
					}
				}
				if (materialO.texture){
					albedoTexture.skin=ResFileManager.getIDEResPath(materialO.texture);
				};
				var vectors=lmatProp.vectors;
				var vector;
				for (var j=0;j < vectors.length;j++){
					vector=vectors[j];
					if (vector.name=="albedoColor"){
						albedoColor.setColorArr(vector.value);
					}
				}
				if (materialO.color){
					albedoColor.setColorArr(materialO.color);
				}
			}
		}

		__proto.getPath=function(basePath,relativePath){
			var baseArray=basePath.split('\\');
			var relativePath=relativePath.splice('/');
		}

		__proto.initByConfig=function(configO){
			_super.prototype.initByConfig.call(this,configO);
			if (configO.useAssetsPath){
				this.useAssetsPath=configO.useAssetsPath;
			}
		}

		LayaMaterialUI.__init$=function(){
			PropsInputBase.regPlugin("LayaMaterialUI",LayaMaterialUI);;
		}

		return LayaMaterialUI;
	})(PropsInputBase)


	Laya.__init([LayaMaterialUI]);
})(window,document,Laya);
